package com.ppandroid.wifiap.adapter

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.ppandroid.wifiap.R
import com.ppandroid.wifiap.model.ConnectedDevice

/**
 * 连接设备列表适配器
 */
class ConnectedDevicesAdapter : RecyclerView.Adapter<ConnectedDevicesAdapter.DeviceViewHolder>() {
    
    private val devices = mutableListOf<ConnectedDevice>()
    
    /**
     * 更新设备列表
     */
    fun updateDevices(newDevices: List<ConnectedDevice>) {
        devices.clear()
        devices.addAll(newDevices)
        notifyDataSetChanged()
    }
    
    /**
     * 添加设备
     */
    fun addDevice(device: ConnectedDevice) {
        if (!devices.contains(device)) {
            devices.add(device)
            notifyItemInserted(devices.size - 1)
        }
    }
    
    /**
     * 移除设备
     */
    fun removeDevice(device: ConnectedDevice) {
        val index = devices.indexOf(device)
        if (index != -1) {
            devices.removeAt(index)
            notifyItemRemoved(index)
        }
    }
    
    /**
     * 清空设备列表
     */
    fun clearDevices() {
        val size = devices.size
        devices.clear()
        notifyItemRangeRemoved(0, size)
    }
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DeviceViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_connected_device, parent, false)
        return DeviceViewHolder(view)
    }
    
    override fun onBindViewHolder(holder: DeviceViewHolder, position: Int) {
        holder.bind(devices[position])
    }
    
    override fun getItemCount(): Int = devices.size
    
    /**
     * 设备ViewHolder
     */
    class DeviceViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        
        private val tvDeviceName: TextView = itemView.findViewById(R.id.tvDeviceName)
        private val tvMacAddress: TextView = itemView.findViewById(R.id.tvMacAddress)
        private val tvAssignedIp: TextView = itemView.findViewById(R.id.tvAssignedIp)
        private val btnCopyIp: ImageButton = itemView.findViewById(R.id.btnCopyIp)
        
        fun bind(device: ConnectedDevice) {
            tvDeviceName.text = device.deviceName
            tvMacAddress.text = device.macAddress
            tvAssignedIp.text = device.assignedIp
            
            // 设置复制IP地址按钮点击事件
            btnCopyIp.setOnClickListener {
                val clipboardManager = itemView.context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
                val clipData = ClipData.newPlainText("IP地址", device.assignedIp)
                clipboardManager.setPrimaryClip(clipData)
                
                // 显示复制成功提示
                Toast.makeText(itemView.context, "IP地址已复制到剪贴板", Toast.LENGTH_SHORT).show()
                
                // 添加按钮动画效果
                btnCopyIp.animate()
                    .scaleX(0.8f)
                    .scaleY(0.8f)
                    .setDuration(100)
                    .withEndAction {
                        btnCopyIp.animate()
                            .scaleX(1f)
                            .scaleY(1f)
                            .setDuration(100)
                            .start()
                    }
                    .start()
            }
        }
    }
}
